package ocean.chinacraft.common.world.item.spell;

import net.minecraft.entity.Entity;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.goal.NearestAttackableTargetGoal;
import net.minecraft.entity.monster.MonsterEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import ocean.chinacraft.common.config.GroupDefine;

/**
 * 混沌符
 *
 * @author zhang
 * @since 2021/12/15
 */
public class ChaoticSpell extends Item {

    public ChaoticSpell() {
        super(new Properties().tab(GroupDefine.CHINACRAFT_SPELL));
    }

    @Override
    public boolean onLeftClickEntity(ItemStack stack, PlayerEntity player, Entity entity) {
        if (player.isAlive() && entity.isAlive() && entity instanceof MonsterEntity) {
            MonsterEntity monster = (MonsterEntity) entity;
            monster.targetSelector.addGoal(0, new NearestAttackableTargetGoal<>(monster, MobEntity.class, false));
        }
        if (!player.isCreative()) {
            stack.shrink(1);
        }
        return true;
    }
}
